package com.fire.controller;


import com.fire.api.MerchantService;
import com.fire.api.dto.MerchantDTO;
import com.fire.common.domain.BusinessException;
import com.fire.common.domain.CommonErrorCode;
import com.fire.common.util.PhoneUtil;
import com.fire.common.util.StringUtil;
import com.fire.common.utils.SecurityUtil;
import com.fire.converter.MerchantDetailConverter;
import com.fire.converter.MerchantRegisterConverter;
import com.fire.service.FileService;
import com.fire.service.SmsService;
import com.fire.vo.MerchantDetailVO;
import com.fire.vo.MerchantRegisterVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-07-09
 */
@Slf4j
@RestController
@Api(value = "商户平台接口", tags = "商户平台接口", description = "")
public class MerchantController {

    @DubboReference
    private MerchantService merchantService;

    @GetMapping("/merchant/{id}")
    public MerchantDTO heeh(@PathVariable("id") long id) {
        MerchantDTO merchantById = merchantService.findMerchantById(id);
        System.out.println(merchantById);
        return merchantById;
    }

    @Autowired
    private SmsService smsService;

    @ApiOperation("获得手机验证码")
    @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String", paramType = "query")
    @GetMapping("/sms")
    public String getSmsCode(@RequestParam("phone") String mobile) {
        return smsService.sendMsg(mobile);
    }

    @ApiOperation("商户注册")
    @PostMapping("/merchant/register")
    @ApiImplicitParam(value = "商户注册信息", name = "merchantRegisterVO", required = true, dataType = "MerchantRegisterVO", paramType = "body")
    public MerchantRegisterVO merchantRegisterVO(@RequestBody MerchantRegisterVO merchantRegisterVO) {
//        校验参数合法性
        if (merchantRegisterVO==null){
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        if (StringUtil.isBlank(merchantRegisterVO.getMobile())){
            throw new BusinessException(CommonErrorCode.E_100112);
        }
//        校验手机号格式
        if (PhoneUtil.isMatches(merchantRegisterVO.getMobile())){
            throw new BusinessException(CommonErrorCode.E_100109);
        }
//        1.校验验证码
        smsService.verify(merchantRegisterVO.getVerifiyKey(), merchantRegisterVO.getVerifiyCode());
//        2.注册商户
//        MerchantDTO merchantDTO = new MerchantDTO();
//        merchantDTO.setMobile(merchantRegisterVO.getMobile());
//        merchantDTO.setUsername(merchantRegisterVO.getUsername());
        //.....填充数据
        /**
         * 采用MapStruct进行数据转换
         */
        MerchantDTO merchantDTO = MerchantRegisterConverter.instance.MerchantRegisterVO2MerchantDto(merchantRegisterVO);
        merchantService.createMerchant(merchantDTO);
        return merchantRegisterVO;

    }

    @Autowired
    private FileService fileService;

    /**
     * 上传证件照
     * @return
     */
    @ApiOperation("证件上传")
    @PostMapping("/upload")
    public String upload(@ApiParam(value = "上传的文件",required = true) @RequestParam("file")MultipartFile file) throws IOException {
//        生成文件名 ,保证唯一性
//        扩展名
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".")-1);
        String key = UUID.randomUUID() + suffix;
        return fileService.Upload(file.getBytes(), key);
    }

    @ApiOperation("资质申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchantInfo", value = "商户认证资料", required = true,
                    dataType = "MerchantDetailVO", paramType = "body")
    })
    @PostMapping("/my/merchant/save")
    public void saveMerchant(@RequestBody MerchantDetailVO merchantDetailVO){
//        解析token，去除当前的商户ID
        Long merchantId = SecurityUtil.getMerchantId();
        System.out.println(merchantId);
//        利用mapstruct转换VO--->DTO
        MerchantDTO merchantDTO = MerchantDetailConverter.instance.MerchantDetailVO2MerchantDto(merchantDetailVO);

//        添加信息
        merchantService.applyMerchant(merchantId,merchantDTO);
    }
}
